﻿using AppDesigner.File;
using AppDesigner.Model;
using System;
using System.Collections.Generic;
using System.Xml;

namespace AppDesigner.XmlParser
{
    public class LayoutReader
    {
        #region Fields
        private XmlDocument _document;
        private string _xml;
        #endregion


        public void Read(string filePath)
        {
            _xml = FileHelper.ReadSolutionFile(filePath);
            _document = new XmlDocument();
            _document.LoadXml(_xml);
        }

        public List<LayoutInfo> GetHeader()
        {
            XmlNodeList nodeList = _document.GetElementsByTagName("Header");

            XmlNode headerNode = nodeList[0];

            List<LayoutInfo> layoutInfos = new List<LayoutInfo>();

            foreach (XmlNode node in headerNode.ChildNodes)
            {
                layoutInfos.Add(ReadLayout(node));
            }

            return layoutInfos;
        }

        public List<LayoutInfo> GetBody()
        {
            XmlNodeList nodeList = _document.GetElementsByTagName("Body");

            XmlNode headerNode = nodeList[0];

            List<LayoutInfo> layoutInfos = new List<LayoutInfo>();

            foreach (XmlNode node in headerNode.ChildNodes)
            {
                layoutInfos.Add(ReadLayout(node));
            }

            return layoutInfos;
        }

        public LayoutInfo GetBar()
        {
            throw new NotImplementedException();
        }

        private LayoutInfo ReadLayout(XmlNode node)
        {
            LayoutInfo layoutInfo = new LayoutInfo();

            OperateNode(node, layoutInfo);

            if (node.ChildNodes.Count > 0)
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    LayoutInfo childLayoutInfo = ReadLayout(childNode);
                    layoutInfo.ChilldInfos.Add(childLayoutInfo);
                }
            }

            return layoutInfo;
        }

        private void OperateNode(XmlNode node, LayoutInfo layoutInfo)
        {
            layoutInfo.ControlType = node.Name;

            if (node.Attributes["Name"] != null)
            {
                layoutInfo.Name = node.Attributes["Name"].Value;
            }

            if (node.Attributes["Value"] != null)
            {
                layoutInfo.Value = node.Attributes["Value"].Value;
            }

            if (node.Attributes["Alignment"] != null)
            {
                layoutInfo.Alignment = node.Attributes["Alignment"].Value;
            }

            if (node.Attributes["Margin"] != null)
            {
                layoutInfo.Margin = node.Attributes["Margin"].Value;
            }

            if (node.Attributes["ControlColor"] != null)
            {
                layoutInfo.ControlColor = node.Attributes["ControlColor"].Value;
            }

            if (node.Attributes["TextColor"] != null)
            {
                layoutInfo.TextColor = node.Attributes["TextColor"].Value;
            }

            if (node.Attributes["ItemsSource"] != null)
            {
                layoutInfo.ItemsSource = node.Attributes["ItemsSource"].Value;
            }

            if (node.Attributes["Width"] != null)
            {
                layoutInfo.Width = node.Attributes["Width"].Value;
            }

            if (node.Attributes["Height"] != null)
            {
                layoutInfo.Height = node.Attributes["Height"].Value;
            }

            if (node.Attributes["Orientation"] != null)
            {
                layoutInfo.Orientation = node.Attributes["Orientation"].Value;
            }
        }
    }
}
